package cn.neusoft.servlet;

import cn.neusoft.bean.User;
import cn.neusoft.query.UserQuery;
import cn.neusoft.service.IUserService;
import cn.neusoft.service.impl.UserService;
import cn.neusoft.utils.IOUtils;
import cn.neusoft.utils.JsonUtils;
import cn.neusoft.vo.ResultBean;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("-------UserServlet------");
        IUserService userService = new UserService();
        String what = req.getParameter("what");
        ResultBean resultBean = null;
        try {
            if("save".equals(what)){
                System.out.println("添加或修改用户");
                String jsonStr = IOUtils.readStr(req.getInputStream());
                User user = JsonUtils.jsonStrToObj(jsonStr, "yyyy-MM-dd",User.class);
                if (user.getId()==null){
                    resultBean = userService.add(user);
                }else {
                    resultBean = userService.update(user);
                }
            }else if("del".equals(what)){
                System.out.println("删除用户");
                String id = req.getParameter("id");
                resultBean = userService.delete(Integer.parseInt(id));
            }else if("getById".equals(what)){
                System.out.println("通过id查询用户");
                String id = req.getParameter("id");
                resultBean = userService.getById(Integer.parseInt(id));
            }else if("page".equals(what)){
                System.out.println("分页搜索用户");
                String jsonStr = IOUtils.readStr(req.getInputStream());
                UserQuery userQuery = JsonUtils.jsonStrToObj(jsonStr, null, UserQuery.class);
                resultBean = userService.getByPage(userQuery);
            } else if ("batchDel".equals(what)) {
                //批量删除获取
                String jsonStr = IOUtils.readStr(req.getInputStream());
                JSONArray jsonArray = JSONObject.parseArray(jsonStr);
                List<Integer> ids = jsonArray.toJavaList(Integer.class);
                System.out.println("json转集合："+ids);
                resultBean = userService.batchDel(ids);
            } else {
                System.out.println("无服务");
                resultBean = ResultBean.ERROR("no service");
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultBean = ResultBean.ERROR(e.getMessage());
        }
        String resultJsonStr = JSONObject.toJSONString(resultBean);
        resp.getWriter().write(resultJsonStr);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
